package landian.com.myapplication11;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import landian.com.module_base.BaseFragment;
import landian.com.module_base.BaseFragmentActivity;

/**
 * Created by Administrator on 2019-03-04.
 */

public class TestActivity extends BaseFragmentActivity<TestPresenter> implements ITestView {
    @BindView(R.id.tv1)
    TextView mTv1;
    @BindView(R.id.tv2)
    TextView mTv2;

    private FragmentManager mFragmentManager;
    private FragmentOne mFragmentOne;
    private FragmentTwo mFragmentTwo;

    @Override
    protected int getContentView() {
        return R.layout.activity_test;
    }

    @Override
    protected void initView(Intent intent) {
        mFragmentManager = getSupportFragmentManager();
        mFragmentOne = new FragmentOne();
        mFragmentTwo = new FragmentTwo();
        changeFragment(mFragmentOne);
        mTv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        mTv1.setOnClickListener(view -> {
            String s = "dd";
             mTv2.setText(s);
        });
    }

    @Override
    protected TestPresenter initPresenter() {
        return new TestPresenter();
    }


    @OnClick({R.id.tv1, R.id.tv2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv1:
                changeFragment(mFragmentOne);
                break;
            case R.id.tv2:
                changeFragment(mFragmentTwo);
                break;
        }
    }

    @Override
    protected int setFragmentView() {
        return R.id.fm;
    }

    private BaseFragment defaultFragment;

    private void changeFm(BaseFragment fragment) {
//        FragmentTransaction transaction = mFragmentManager.beginTransaction();
//        if (defaultFragment != null && !defaultFragment.isHidden()) {
//            transaction.hide(defaultFragment);
//        }
//        if (!fragment.isAdded()) {
//            transaction.add(R.id.fm, fragment);
//        } else {
//            transaction.show(fragment);
//        }
//        defaultFragment = fragment;
//        transaction.commit();

//        if (!fragment.isAdded()) {
//            mFragmentManager.beginTransaction().hide(defaultFragment).add(R.id.fm, fragment, fragment.getTag()).commit();
//        } else {
//            mFragmentManager.beginTransaction().hide(defaultFragment).show(fragment).commit();
//        }
//        defaultFragment = fragment;

    }
}
